package cn.piaoyi.web.action;

import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.web.servlet.AdminSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminCategory extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminCategory.class);
	
	private CategoryService categoryService;

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}


	private long id;
	private String name;
	private String code;
	private int orders;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getOrders() {
		return orders;
	}

	public void setOrders(int orders) {
		this.orders = orders;
	}
	
	private List categoryList;

	public List getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List categoryList) {
		this.categoryList = categoryList;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		List l = this.getCategoryService().findCategorys("orders", 0);
		this.setCategoryList(l);
		return SUCCESS;
	}
	
	public String add(){
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		this.name = StringUtils.trimToEmpty(this.name);
		this.code = StringUtils.trimToEmpty(this.code);
		//添加用户
		Category c = new Category();
		c.setName(this.name);
		c.setCode(this.code);
		c.setOrders(this.orders);
		try {
			this.getCategoryService().createCategory(c);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		this.setAction("doupdate");
		Category c = this.getCategoryService().findCategoryById(this.id);
		this.setName(c.getName());
		this.setCode(c.getCode());
		this.setOrders(c.getOrders());
		return INPUT;
	}
	
	public String doupdate(){
		this.name = StringUtils.trimToEmpty(this.name);
		this.code = StringUtils.trimToEmpty(this.code);

		Category c = this.getCategoryService().findCategoryById(this.id);
		c.setName(this.name);
		c.setCode(this.code);
		c.setOrders(this.orders);
		try {
			this.getCategoryService().updateCategory(c);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getCategoryService().removeCategoryById(this.id);
		return "index";
	}


}
